<template>
	<div class="mask">
		<div class="changing">
			<div class="p-30" style="position: relative;">
				<div class="text-white fz48 font-bold">
					正在充电
				</div>
				<div class="flex"
					style="position: absolute;color: #62D2A7; display: none; font-size: 80rpx;z-index: 9;align-items: flex-end;right:20rpx;top:50rpx">
					<div class="percent font-bold">{{val.percentage}}</div>
					<div class="fz30" style="margin-bottom: 15rpx;margin-left: 10rpx;">%</div>
				</div>
			</div>
			<image src="https://app.yinuoxinnengyuan.cn/static/car2.png" class="w-full" mode="widthFix"></image>
			<div class="p-30">
				<div class="between">
					<div class="center flex-1 flex-col">
						<div class="text-white">
							当前充电金额
						</div>
						<div class="fz30 text-white fz50" style="color: #C4FEFF;">
							{{val.charging_amount}} <text style="font-size: 26rpx;">元</text>
						</div>
					</div>
					<div class="center flex-1 flex-col">
						<div class="text-white">
							当前充电量
						</div>
						<div class="fz30 text-white fz50" style="color: #C4FEFF;">
							{{val.charging_capacity}} <text style="font-size: 26rpx;">度</text>
						</div>
					</div>
				</div>

				<div class="between mt-50 pl-20 pr-20">
					<div class="flex-1 flex-col" style="color: #3662EC;">
						<div>
							实时电流
						</div>
						<div class="fz30  fz45">
							{{val.charging_electric}}
						</div>
					</div>
					<div class="flex-1 flex-col" style="color: #62D2A7;">
						<div class="">
							实时电压
						</div>
						<div class="fz30  fz45">
							{{val.charging_voltage}}
						</div>
					</div>
					<div class="flex-1 flex-col" style="color: #FA5151;">
						<div class="">
							已充电时长
						</div>
						<div class="fz30  fz45">
							{{val.charging_duration}}分
						</div>
					</div>
				</div>
				
				<div class="between mt-50 pl-20 pr-20" style="display: none;">
					<div class="flex-1 flex-col" style="color: #3662EC;">
						<div>
							充电状态
						</div>
						<div class="fz30  fz45">
							{{val.dev_status?val.dev_status:'离线'}}
						</div>
					</div>
					<div class="flex-1 flex-col" style="color: #62D2A7;">
						<div class="">
							插枪状态
						</div>
						<div class="fz30  fz45">
							{{val.dev_qiang_status?val.dev_qiang_status:'否'}}
						</div>
					</div>
				</div>
				
				
				<div class="fz36 center" v-if="type==0" @click="start(val.id)"
					style="color: green;margin-top: 50rpx; width: 100%; text-align: center; margin-bottom: 10rpx;display: none;">
					重启上电
				</div>

				<div class="fz50 center" v-if="type==0" @click="end(val.id)"
					style="color: #F90000;margin-top: 30rpx;padding-left: 70rpx;margin-bottom: 20rpx;">
					结束充电
					<div class="flex" style="margin-top: 10rpx;margin-left: -5rpx;">
						<image src="/static/icon/right.png" mode="widthFix" style="width: 48rpx;"></image>
						<image src="/static/icon/right.png" mode="widthFix"
							style="width: 48rpx;position: relative;left:-30rpx"></image>
					</div>
				</div>
				
				<div @click="close" style="text-align: center; margin-top: 20px;">
					<image src="/static/icon/close.png" mode="widthFix"
						style="width: 80rpx;"></image>
				</div>

			</div>
		</div>
	</div>
</template>

<script>
	export default {
		props: ["show","item","type"],
		data() {
			return {
				val:{},
				intervalId: null
			}
		},
		mounted() {
			this.val = this.item
			this.lookat();
		},
		beforeDestroy() { // 对于 Vue 2 组件
		    this.clearTimer();
		},
		methods: {
			close() {
				this.$emit("update:show", false)
			},
			lookat(){
				var that = this
				var id = this.val.id
				this.intervalId = setInterval(function(){
					that.order(id)
				},3000)
			},
			clearTimer() {
			  if (this.intervalId) {
				clearInterval(this.intervalId);
				this.intervalId = null; // 清除引用，帮助垃圾回收
			  }
			},
			async order(id){
				var that = this
				const res = await this.http("post", "/api/order/info", {
					id: id,
				})
				this.val = res.data
			},
			async start(id){
				var that = this
				const res = await this.http("post", "/api/order/charging_order", {
					id: id,
				})
				uni.showToast({
					title:'操作成功'
				})
			},
			end(id){
				var that = this
				
				uni.showModal({
				    title: '提示',
				    content: '确定结束充电吗？',
				    success: function (res) {
				        if (res.confirm) {
				            const res = that.http("post", "/api/order/topay_order", {
								id: id,
							})
							uni.showToast({
								title:'结束成功'
							})
							setTimeout(function(){
								that.$emit("update:show", false)
								that.$emit("update", "update")
							},1000)
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});

				
			}
		}
	}
</script>

<style lang="less">
	.mask {
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 999;
		position: fixed;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.changing {
		width: 90%;
		background-color: #000;
		border-radius: 20rpx;
	}
</style>